// TODO set tpl cache.
// TODO more path setup and error process.
dojo.provide("lotex.system.Render");

dojo.require("dojo.lang.declare");
dojo.require("dojo.event.*");
dojo.require("lotex.system.Loader");
dojo.require("lotex.system.AsynControler");

dojo.declare("lotex.system.Render", null, {
    initializer: function(/*String*/ domid, /*String*/ path) {
        //this.template = null;
        this.domid = domid;
        this.jsPath = lotex.system.Uri(path+".js");
        this.jsonPath = lotex.system.Uri(path+".json");
        this.templatePath = lotex.system.Uri(path+".tpl");

        // TODO: more arg type;
        //if(dojo.lang.isString(arguments[0]))
    },

    process: function(instance) {
        //dd(this);
        //d();
        //d(this.templatePath + " [in process]: " + this.template);
        //d();
        //d(this.jsPath + " [in process]: " + this.script);
        //d();
        //d(this.jsonPath + " [in process]: " + this.data);
        //d();
        if(this.haveTpl && this.haveData) {
            var result = this.template.process(this.data);
            dojo.byId(this.domid).innerHTML = result;
        }
        if(this.haveTpl && !this.haveData) {
            var result = this.template;
            dojo.byId(this.domid).innerHTML = result;
        }
        if(this.haveScript) {
            dj_eval(this.script);
        }

        this.renderComplete();
    },

    render: function() {
        var __self = this;

        var asynControler = new lotex.system.AsynControler(3);

        var tpl = dojo.io.bind({
            url: __self.templatePath,
            error: function(type, data, evt){
                __self.haveTpl = false;
                asynControler.remove("tpl");
                dd(evt);
                d("error tpl >>>> " + asynControler.signal);
            },
            load: function(type, data, evt) {
                __self.haveTpl = true;
                __self.template = data;
                asynControler.remove("tpl");
                d("load tpl @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " + asynControler.signal);
                d(__self.templatePath + " : " + __self.template);
            },
            mimetype: "text/plain"
        });



        var json = dojo.io.bind({
            url: __self.jsonPath,
            error: function(type, data, evt){
                __self.haveData = false;
                asynControler.remove("json");
                //d("error json >>>> " + asynControler.signal);
            },
            load: function(type, data, evt) {
                __self.haveData = true;
                __self.data = data;
                asynControler.remove("json");
                //d("load json @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " + asynControler.signal);
                //d(__self.jsonPath + " : " + __self.data);
            },
            mimetype: "text/json"
        });

        var cmd = dojo.io.bind({
            url: __self.jsPath,
            error: function(type, data, evt){
                __self.haveScript = false;
                asynControler.remove("cmd");
                //d("error cmd >>>> " + asynControler.signal);
            },
            load: function(type, data, evt) {
                __self.haveScript = true;
                __self.script = data;
                asynControler.remove("cmd");
                //d("load cmd @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " + asynControler.signal);
            },
            mimetype: "text/plain"
        });
        dojo.event.connect(asynControler, "complete", function() {
            __self.process(__self);
        });
    },

    renderComplete: function() {
    }
});